package com.abo.yaas.view.widgets.fields;

import com.abo.yaas.domain.Category;
import com.abo.yaas.webservice.ResourceProvider;
import com.abo.yaas.webservice.client.EntityProviderProxy;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.validator.NullValidator;
import com.vaadin.ui.Select;

@SuppressWarnings("serial")
public class CategoryCombo extends Select {
    final BeanItemContainer<Category> container = new BeanItemContainer<Category>(
            Category.class);

    public CategoryCombo() {
        this("");
    }

    public CategoryCombo(String caption) {
        super(caption);
        final EntityProviderProxy<Category> proxy = ResourceProvider
                .getEntityProvider(Category.class);
        for (int i = 0; i < proxy.getEntityCount(null); ++i) {
            container.addBean(proxy.getEntity(proxy.getEntityIdentifierAt(null,
                    null, i)));
        }
        setWriteThrough(true);
        setContainerDataSource(container);
        setNullSelectionAllowed(false);
        setImmediate(true);
        setItemCaptionPropertyId("title");
        setWidth("100%");
        setRequired(true);
        addValidator(new NullValidator("You must chose category!", false));
    }
}